﻿using System.Text;

/// <summary>
/// 简单的分页
/// </summary>
public class LvEasyPager
{
    private static string _pathRoot = "../images";
    public LvEasyPager()
    {

    }
    /// <summary>
    /// 最简单的分页
    /// </summary>
    /// <param name="url">格式化URL,abc.com/page/{0}</param>
    /// <param name="pageindex">当前页</param>
    /// <param name="pagesize">每页记录数</param>
    /// <param name="total">总记录数</param>
    /// <returns></returns>
    public static string Create(string url, int pageindex, int pagesize, int total)
    {

        StringBuilder strPageHtml = new StringBuilder();

        string urlFormat = @"<span class='{0}'><a href='{1}'>{2}</a></span> ";

        string urlFormat2 = @"<span class='{0}' {3}>{2}</span> ";

        int pageNum = total / pagesize;

        if (total % pagesize > 0) pageNum++;

        if (pageindex == 0)
            pageindex = 1;

        strPageHtml.Append(" 每页:");

        strPageHtml.Append(dealPageSize(10, url, pageindex, pagesize, total));
        strPageHtml.Append(dealPageSize(20, url, pageindex, pagesize, total));
        strPageHtml.Append(dealPageSize(30, url, pageindex, pagesize, total));
        strPageHtml.Append(dealPageSize(50, url, pageindex, pagesize, total));
        if (pagesize != 10 && pagesize != 20 && pagesize != 30 && pagesize != 50)
        {
            strPageHtml.Append(" " + pagesize);
        }
        strPageHtml.Append(" 页次:" + pageindex + "/" + pageNum + " 总数:" + total + " ");

        //HttpContext.Current.Response.Write(pageNum.ToString());

        //if (total <= pagesize) return strPageHtml.ToString();

        //首页
        if (pageindex <= 1)
        {
            //strPageHtml += "<li class=\"p10\"><a href=\"" + string.Format(url, abs) + "\">";
            strPageHtml.Append(string.Format(urlFormat2, "first", string.Format(url, "1", pagesize), "<a><img src='" + _pathRoot + "/page/first_dis.gif' width='21' height='16' border='0' align='absmiddle' style='display:inline;' /></a>", "disabled=\"true\""));

        }
        else
        {
            strPageHtml.Append(string.Format(urlFormat, "first", string.Format(url, "1", pagesize), "<img src='" + _pathRoot + "/page/first.gif' width='21' height='16' border='0' align='absmiddle' style='display:inline;' />", ""));
        }

        //上一页
        if (pageindex < 1) pageindex = 1;

        if (pageindex == 1)
        {
            //strPageHtml += "<li class=\"p10\"><a href=\"" + string.Format(url, abs) + "\">;
            strPageHtml.Append(string.Format(urlFormat2, "pre", string.Format(url, (pageindex - 1).ToString(), pagesize), "<a><img src='" + _pathRoot + "/page/pre_dis.gif' width='41' height='16' border='0' align='absmiddle' style='display:inline;' /></a>", "disabled=\"true\""));

        }
        else
        {
            strPageHtml.Append(string.Format(urlFormat, "pre", string.Format(url, (pageindex - 1).ToString(), pagesize), "<img src='" + _pathRoot + "/page/pre.gif' width='41' height='16' border='0' align='absmiddle' style='display:inline;' />", ""));
        }

        //Loop
        int pre = pageindex - 5;
        if (pre <= 0)
        {
            pre = 1;
        }
        for (int i = pre; i < pageindex; i++)
        {
            strPageHtml.Append(string.Format(urlFormat, "page", string.Format(url, i.ToString(), pagesize), i.ToString(), ""));
        }


        //当前页
        strPageHtml.Append(string.Format(urlFormat2, "abs", string.Format(url, pageindex.ToString(), pagesize), "<a>" + pageindex.ToString() + "</a>", "disabled=\"true\""));

        //loop下5页
        int next = pageindex + 5;
        if (next > pageNum) next = pageNum;

        for (int i = pageindex + 1; i < next + 1; i++)
        {
            strPageHtml.Append(string.Format(urlFormat, "page", string.Format(url, i.ToString(), pagesize), i.ToString(), ""));
        }


        //下一页
        if (pageindex == pageNum)
        {
            //strPageHtml += "<li class=\"p10\"><a href=\"" + string.Format(url, abs) + "\">;
            strPageHtml.Append(string.Format(urlFormat2, "pre", string.Format(url, (pageindex + 1).ToString(), pagesize), "<a><img src='" + _pathRoot + "/page/next_dis.gif' width='41' height='16' border='0' align='absmiddle' style='display:inline;' /></a>", "disabled=\"true\""));

        }
        else
        {
            strPageHtml.Append(string.Format(urlFormat, "pre", string.Format(url, (pageindex + 1).ToString(), pagesize), "<img src='" + _pathRoot + "/page/next.gif' width='41' height='16' border='0' align='absmiddle' style='display:inline;' />", ""));
        }


        //尾页
        if (pageindex == pageNum)
        {
            //strPageHtml += "<li class=\"p10\"><a href=\"" + string.Format(url, abs) + "\">;
            strPageHtml.Append(string.Format(urlFormat2, "last", string.Format(url, pageNum.ToString(), pagesize), "<a><img src='" + _pathRoot + "/page/last_dis.gif' width='21' height='16' border='0' align='absmiddle' style='display:inline;' /></a>", "disabled=\"true\""));

        }
        else
        {
            strPageHtml.Append(string.Format(urlFormat, "last", string.Format(url, pageNum.ToString(), pagesize), "<img src='" + _pathRoot + "/page/last.gif' width='21' height='16' border='0' align='absmiddle' style='display:inline;' />", ""));
        }

        return strPageHtml.ToString();
    }

    private static string dealPageSize(int dealPageSize, string url, int pageindex, int pagesize, int total)
    {
        string v = "";
        

        if (pagesize == dealPageSize)
        {
            v = " <font color='gray'>"+pagesize.ToString()+"</font>";
        }
        else
        {
            int pageNum = total / dealPageSize;
            if (total % pagesize > 0) pageNum++;
            if (pageindex > pageNum) pageindex = pageNum;
            v = " <a href='" + string.Format(url, pageindex, dealPageSize) + "'>" + dealPageSize + "</a>";
        }

        return v;
    }
    /// <summary>
    /// 用户中心分页
    /// </summary>
    /// <param name="url"></param>
    /// <param name="pageindex"></param>
    /// <param name="pagesize"></param>
    /// <param name="total"></param>
    /// <returns></returns>
    public static string CreateUser(string url, int pageindex, int pagesize, int total)
    {
        int pageNum = total / pagesize;
        if (total % pagesize > 0) pageNum++;
        if (pageindex < 1) pageindex = 1;
        StringBuilder sb = new StringBuilder();
        sb.AppendFormat("共有 {0} 个数据&nbsp;&nbsp;分页：&nbsp;", total);
        int pre = pageindex - 5;
        if (pre < 1) pre = 1;
        for (int i = pre; i < pageindex; i++)
        {
            sb.AppendFormat("<a href='{0}pi={1}&pz={2}'>{1}</a>&nbsp;", url, i, pagesize);
        }
        sb.AppendFormat("<b>{0}</b>&nbsp;", pageindex);
        int next = pageindex + 5;
        if (next > pageNum) next = pageNum;
        for (int i = pageindex + 1; i <= next; i++)
        {
            sb.AppendFormat("<a href='{0}pi={1}&pz={2}'>{1}</a>&nbsp;", url, i, pagesize);
        }
        return sb.ToString();
    }

}

